package com.hm.shopping.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.su.bean.ServerResponse;
import com.hm.shopping.auto.su.ControllerResult;
import com.hm.shopping.auto.su.R.Restful;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.def.GoodsShelfStatusEnums;
import com.hm.shopping.service.GoodsIntroductionService;
import com.hm.shopping.vo.GoodsIntroductionVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月30日 上午10:34:34
 * @version 1.0
 * @describe 
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/goodsint")
public class GoodsIntroductionApi {

	private static final String SWWAGER_TAG = "商品信息管理";
	
	@Autowired
	private GoodsIntroductionService goodsIntroductionService;
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增商品信息", httpMethod = "POST", notes = "com.hm.shopping.api.GoodsIntroductionApi::addGoodsIntroduction")
	public ResponseEntity<ServerResponse<Boolean>> addGoodsIntroduction(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) GoodsIntroductionVo goodsIntroductionVo) {
		log.debug("用户{}新增商品信息", HmSessionFactory.currentUserId());
		goodsIntroductionVo.setCreateUser(HmSessionFactory.currentUserId());
		goodsIntroductionVo.setShopsId(HmSessionFactory.currentUserId()+"");
		goodsIntroductionService.addGoodsIntroduction(goodsIntroductionVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{goodsId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取商品信息", httpMethod = "GET", notes = "com.hm.shopping.api.GoodsIntroductionApi::getGoodsIntroduction")
	public ResponseEntity<ServerResponse<GoodsIntroductionVo>> getGoodsIntroduction(
			@ApiParam(required = true, name = "goodsId", value = "商品ID") @PathVariable("goodsId") String goodsId) {
		log.debug("用户{}获取商品信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(goodsIntroductionService.getGoodsIntroduction(goodsId));
	}

	@RequestMapping(value = "/{goodsId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改商品信息", httpMethod = "PUT", notes = "com.hm.shopping.api.GoodsIntroductionApi::updateGoodsIntroduction")
	public ResponseEntity<ServerResponse<Boolean>> updateGoodsIntroduction(
			@ApiParam(required = true, name = "goodsId", value = "商品ID") @PathVariable("goodsId") String goodsId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) GoodsIntroductionVo goodsIntroductionVo) {
		log.debug("用户{}修改商品信息", HmSessionFactory.currentUserId());
		goodsIntroductionVo.setId(goodsId);
		goodsIntroductionService.updateGoodsIntroduction(goodsIntroductionVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除商品信息", httpMethod = "DELETE", notes = "com.hm.shopping.api.GoodsIntroductionApi::deleteGoodsIntroduction")
	public ResponseEntity<ServerResponse<Boolean>> deleteGoodsIntroduction(
			@ApiParam(required = true, name = "goodsId", value = "商品ID") @RequestParam(required = true, name = "goodsId") String goodsId) {
		log.debug("用户{}删除商品信息", HmSessionFactory.currentUserId());
		goodsIntroductionService.deleteGoodsIntroduction(goodsId);
		return ControllerResult.success(true);
	}
	
	@RequestMapping(value = "/{goodsId}", method = RequestMethod.PATCH)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改商品上架状态", httpMethod = "PATCH", notes = "com.hm.shopping.api.GoodsIntroductionApi::updateGoodsIntroductionStatus")
	public ResponseEntity<ServerResponse<Boolean>> updateGoodsIntroductionStatus(
			@ApiParam(required = true, name = "goodsId", value = "商品ID") @PathVariable("goodsId") String goodsId,
			@ApiParam(required = true, name = "goodsShelfStatus", value = "上架状态") @RequestParam(required = true, name = "goodsShelfStatus") GoodsShelfStatusEnums goodsShelfStatus){
		log.debug("用户{}修改商品上架状态", HmSessionFactory.currentUserId());
		goodsIntroductionService.updateGoodsIntroductionStatus(goodsId, goodsShelfStatus);
		return ControllerResult.success(true);
	}
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页商品信息记录", httpMethod = "GET", notes = "com.hm.shopping.api.GoodsIntroductionApi::queryGoodsIntroduction")
	public ResponseEntity<ServerResponse<PageInfo<GoodsIntroductionVo>>> queryGoodsIntroduction(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "goodsName", value = "商品名称") @RequestParam(required = false, name = "goodsName") String goodsName){
		log.debug("用户{},查询分页商品信息记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(goodsIntroductionService.queryGoodsIntroduction(new PageInfoParam(pageNumber, pageSize), HmSessionFactory.currentUserId()+"", goodsName));
	}
}
